﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
 
using MShop.Core.Domain;

namespace MShop.Core.Domain
{
    [MetadataType(typeof(Menu_Validation))]
    public partial class Menu : INode<Menu>
    {
        public string TableRowClass
        {
            get
            {
                if (this.ParentId.HasValue)
                {
                    return string.Concat("child-of-node-", this.Menu2.NodeId);
                }
                else
                    return null;
            }
        }

        public string TableRowId
        {
            get
            {
                return string.Concat("node-", NodeId);
            }
        }
        int? INode.ParentId
        {
            get { return this.ParentId; }
        }
        public bool IsRoot
        {
            get { return !this.ParentId.HasValue; }
        }

        public int Depth
        {
            get
            {
                int i = 0;
                var p = this.Menu2;
                while (p != null)
                {
                    p = p.Menu2;
                    i++;
                }
                return i;
            }
        }

        public int NodeId { get; set; }



        public bool HasChild
        {
            get
            {
                return this.Menu1.Count > 0;
            }
        }

        public RightFlags Right(IEnumerable<ShopUserRights> userrights)
        {
            var right= userrights.FirstOrDefault(x => x.MenuId == this.Id);
            if (right != null)
            {
                return right.RightFlags;
            }
            return RightFlags.NoRight;
        }

        public bool CanView(IEnumerable<ShopUserRights> userrights)
        {
            var right = Right(userrights);
            if ((right & RightFlags.View) != 0)
            {
                return true;
            }
            return false;
        } 

        public Menu Parent
        {
            get { return this.Menu2; }
        }


        public object Key
        {
            get { return this.Id; }
        }


        public IEnumerable<Menu> ChildNodes
        {
            get 
            {
                return this.Menu1;
            }
        }

      
    }

    public class MenuEqualityComparer : IEqualityComparer<Menu>
    {
        
        #region IEqualityComparer<Menu> Members

        public bool Equals(Menu x, Menu y)
        {
            return x.Id.Equals(y.Id);
        }

        public int GetHashCode(Menu obj)
        {
            return obj.Id.GetHashCode();
        }

        #endregion
    }

    public partial class Menu_Validation
    {
        [DisplayName("父菜单")]
        public int ParentId { get; set; }

        [DisplayName("编码")]
        [Required(ErrorMessage = "地址不能为空")]
        public string Code { get; set; }

        [DisplayName("名称")]
        [Required(ErrorMessage = "名称不能为空")]
        public string Name { get; set; }

        [DisplayName("图标")]
       // [Required(ErrorMessage = "图标不能为空")]
        public string Icon { get; set; }

        [DisplayName("地址")]
       // [Required(ErrorMessage="地址不能为空")]
        [DataType(DataType.Url)]
        public string Url { get; set; }
    }
}